#include <iostream>
#include "stdio.h"
using namespace std;
#include <string.h>


//sqlite3头文件
#include "sqlite3.h"

void usage (void) {
	printf("Usage:\r\n");
	printf("blob db_file_name\r\n");
}

int main(int argc, char** argv ) {

	sqlite3 *db;
	char *zErrMsg = 0;
    int rc = 0;
    if ( argc == 2 ) {
		rc = sqlite3_open(argv[1], &db);
	} else {
		usage();
		return 0;
	}

    if( rc ){
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		return 0;
	}else{
		fprintf(stderr, "Opened database successfully\n");
	}
    
    sqlite3_stmt *stmt; 
    /*
    const char* sql = "insert into Faceinfo values(2,'lvsx','/userdata/daijh.jpg',?)";  
    char pPicData[30] = "this is a pic data" ;
    rc = sqlite3_prepare(db,sql,-1,&stmt,0);
    if( rc != SQLITE_OK ){
		fprintf(stderr, "Can't sqlite3_prepare: %s ,rc %d\n", sqlite3_errmsg(db) ,rc);
		return 0;
	}

    rc = sqlite3_bind_blob(stmt, 1, pPicData, 30, NULL);   
    if( rc != SQLITE_OK ){
		fprintf(stderr, "Can't sqlite3_bind_blob: %s ,rc %d\n", sqlite3_errmsg(db) ,rc);
		return 0;
	}

    rc = sqlite3_step(stmt);
    if( rc != SQLITE_DONE ){
		fprintf(stderr, "Can't sqlite3_step: %s ,rc %d\n", sqlite3_errmsg(db) ,rc);
		return 0;
	}
    */
    sqlite3_prepare(db, "select * from Faceinfo", -1, &stmt, 0);
    int result = sqlite3_step(stmt);
    result = sqlite3_step(stmt);
    int id = 0,len = 0; 
    char picData[128] = {0}; 
    if (result == SQLITE_ROW)                                     //查询成功返回的是SQLITE_ROW
    {
        cout<<"read success from sqlite"<<endl;
        id = sqlite3_column_int(stmt, 0);                         //从0开始计算，id为0，picdata 为1；
        const void * pReadPicData = sqlite3_column_blob(stmt, 3); //读取数据，返回一个指针
        len = sqlite3_column_bytes(stmt, 3);                      //返回数据大小
        memcpy(picData, pReadPicData, len);                       //把数据拷贝出来
    }
    else
    {
        cout<<"read fail from sqlite"<<endl;
    }

    cout<<id<<" "<<picData<<endl;
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    
    return 0;
}